package com.pdd.controller;

import com.pdd.beans.Product;
import com.pdd.dto.DTO;
import com.pdd.service.ProductService;
import com.pdd.util.UploadUtil;
import com.pdd.vo.product.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商品控制器
 */
@Api(description = "商品接口",tags = {"商品接口"})
@CrossOrigin(origins = { "*" })
@Controller
public class ProductController {

    @Autowired
    @Setter
    private ProductService productService;

    @ApiOperation(value = "添加商品信息")
    @RequestMapping(value = "/product", method = RequestMethod.POST)
    @ResponseBody
    public DTO addProduct(MultipartFile logo,ProductVO vo,HttpServletRequest request){

        // 1. 执行上传，获取上传后的文件名
        String targetName = UploadUtil.upload(logo, request);
        // 2. 封装参数
        Product product = new Product();
        product.setCid(vo.getCid());
        product.setTitle(vo.getTitle());
        product.setDesc(vo.getDesc());
        product.setPrice_old(vo.getPrice_old());
        product.setPrice_new(vo.getPrice_new());
        product.setCount(vo.getCount());
        product.setDetail(targetName);
        product.setOrder(vo.getOrder());

        // 3. 执行新增
        boolean result = productService.addProduct(product);

        // 4. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }
    @ApiOperation(value = "删除商品")
    @RequestMapping(value = "/product/{id}", method = RequestMethod.DELETE)
    public DTO delProduct(@PathVariable(value = "id") Integer id){

        boolean result = productService.delProduct(id);
        if (result){
            return DTO.success();
        }else {
            return DTO.error();
        }
    }

    @ApiOperation(value = "更新用户信息")
    @RequestMapping(value = "/product", method = RequestMethod.PUT)
    @ResponseBody
    public DTO uplProduct(MultipartFile logo,ProductVO vo,HttpServletRequest request){
        // 1. 执行上传，获取上传后的文件名
        String targetName = UploadUtil.upload(logo, request);
        // 2. 封装参数
        Product product = new Product();
        product.setCid(vo.getCid());
        product.setTitle(vo.getTitle());
        product.setDesc(vo.getDesc());
        product.setPrice_old(vo.getPrice_old());
        product.setPrice_new(vo.getPrice_new());
        product.setCount(vo.getCount());
        product.setDetail(targetName);
        product.setOrder(vo.getOrder());
        // 3. 执行更新
        boolean result = productService.uplProduct(product);

        // 4. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }

    @ApiOperation(value = "查询商品信息")
    @RequestMapping(value = "/product", method = RequestMethod.GET)
    @ResponseBody
    public DTO getProduct(){
        // 1. 执行查询操作
        List<Product> list = productService.getProduct();

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
    @ApiOperation(value = "根据商品分类ID查询商品信息")
    @RequestMapping(value = "/product/{cid}", method = RequestMethod.GET)
    @ResponseBody
    public DTO getById(@PathVariable(value = "cid") Integer cid){
        // 1. 执行查询操作
        List<Product> list = productService.getById(cid);

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
}
